import { reactive, toRefs, getCurrentInstance, h } from "vue"
// ant design
import { message } from "ant-design-vue"
// api
import { frameList, frameStatus, frameDelete } from "@/api"

export function useIndex() {
    // proxy
    const { proxy } = getCurrentInstance()
    // state
    const state = reactive({
        dataList: [],
    })

    /**
     * 获取列表
     */
    const useGetList = (params) => {
        return new Promise((resolve, reject) => {
            frameList(params).then(res => {
                state.dataList = res?.content?.data
                resolve(res)
            })
        })
    }

    /**
     * 删除
     */
    const useDeleteUser = (row) => {
        proxy.$confirm({
            title: "确认删除？",
            content: "是否确认删除该层架",
            okAsync: () => {
              return new Promise((resolve, reject) => { // then，catch
                frameDelete({frame_id: row.frame_id}).then(() => {
                  message.success('删除成功')
                  useGetList()
                  resolve()
                }).catch(() => {
                  reject()
                })
              })
            }
          })
    }

    /**
     * 更新状态
     */
    const useStatusUser = (params) => {
        return new Promise((resolve, reject) => {
            const req = {
                frame_id: params.frame_id,
                frame_status: params.frame_status ? 0 : 1
            }
            frameStatus(req).then((res) => {
                message.success(res?.msg)
                resolve()
            }).catch(() => {
                reject()
            })
        })
    }

    return {
        ...toRefs(state),
        useGetList,
        useDeleteUser,
        useStatusUser
    }
}